package leet;

/**
 * Mirror Reflection
 * 
 * <pre>
 * There is a special square room with mirrors on each of the four walls.  
 * Except for the southwest corner, 
 * there are receptors on each of the remaining corners, 
 * numbered 0, 1, and 2.
 * The square room has walls of length p, 
 * and a laser ray from the southwest corner first meets the east wall at a distance q from the 0th receptor.
 * Return the number of the receptor that the ray meets first.  
 * (It is guaranteed that the ray will meet a receptor eventually.)
 * </pre>
 * 
 * @author zhujunbing
 * @date 2019年4月22日
 */
public class Leet0858 {

	public static void main(String[] args) {
		int mirrorReflection = new Leet0858().mirrorReflection(3, 1);
		System.out.println(mirrorReflection);
	}

	/**
	 * 这个就是求y= (q/p) *x 在 x=ip,y=jp位置, 此时，jp=iq
	 * i和j的最小值为,imin=p/(qp的最大公约数),jmin=q/(qp的最大公约数)
	 * 
	 * 满足下面哪种情况
	 * 
	 * <pre>
	 * 0位置 (p+2ip,2jp)
	 * 1位置(p+2ip,p+2jp)
	 * 2位置(2ip,p+2jp)
	 * </pre>
	 * 
	 * 注意计算的时候把p约掉 看看i%2和j%2的值就知道会到哪里了
	 * 
	 * @param p
	 * @param q
	 * @return
	 * @date 2019年4月22日
	 */
	public int mirrorReflection(int p, int q) {

		int i;
		int j;

		//
		int t = tail(p, q);

		i = p / t;
		j = q / t;

		int a = i % 2;
		int b = j % 2;

		if (a == 1) {

			if (b == 1) {
				return 1;
			} else {
				return 0;
			}

		} else {
			return 2;
		}

	}

	private int tail(int p, int q) {

		if (q == 0) {
			return p;
		}

		return tail(q, p % q);
	}
}
